package com.cnc.fd;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

import com.cnc.bas.db.HiberDao;
import com.cnc.bas.db.JdbcDao;
import com.cnc.bas.util.Log;
import com.cnc.bas.util.SpringBeanFactory;
import com.cnc.fd.hiber.FdCustomerConfig;
 
import com.cnc.fd.hiber.FdTimeoutConfig;
 

/**
 * 文件同步系统的一些相关缓存
 * @author 阿闲
 *
 */
public class WsfdCache{
	
	private static Log log = Log.getLogger(WsfdCache.class);
	
	//客户配置信息
	private static Map<String,FdCustomerConfig> customerConfig = new HashMap<String, FdCustomerConfig>();
	//错误码解析
	private static Map<String,String> errors = new HashMap<String, String>();
	
	// 以Map<taskKind, Map<customer, List<FdTimeoutConfig>>>形式存放
	private static Map<String, Map<String, List<FdTimeoutConfig>>> timeoutConfigs = new HashMap<String, Map<String, List<FdTimeoutConfig>>>();
	
	 
	static{
		init();
	}
	
	public static void init(){
		cacheTimeoutConfig();
		cacheCustomerConfig();
		cacheErrors();		
	 
	}

	
	/**
	 * 缓存客户配置信息
	 */
	public static void cacheCustomerConfig(){
		HiberDao hiberDao = (HiberDao)SpringBeanFactory.getBean(HiberDao.BEAN_ID);
		List<FdCustomerConfig> list = hiberDao.find("from FdCustomerConfig");
		synchronized(customerConfig){
			if(list==null || list.isEmpty()){				
				return;
			}			
			for(int i=0; i<list.size(); i++){
				customerConfig.put(list.get(i).getCustomer(), list.get(i));
			}
		}		
	}
	
	/**
	 * 缓存错误码信息
	 */
	public static void cacheErrors(){
		JdbcDao jdbcDao = (JdbcDao)SpringBeanFactory.getBean(JdbcDao.BEAN_ID);		
		List<Map> list = jdbcDao.queryForList("select * from FD_Errorno");
		synchronized(errors){
			for(Map error : list){
				errors.put(error.get("errorno").toString(), error.get("comment").toString());
			}
		}
		
	}
	
	/**
	 * 缓存客户的文件同步超时管理信息
	 */
	public static void cacheTimeoutConfig(){
		HiberDao hiberDao = (HiberDao)SpringBeanFactory.getBean(HiberDao.BEAN_ID);
		List<FdTimeoutConfig> list = hiberDao.find("from FdTimeoutConfig order by customer, sizeLess");
		  
		synchronized(timeoutConfigs){
			timeoutConfigs.clear();
			for(int i=0; i<list.size(); i++){
				FdTimeoutConfig item = list.get(i);
				if(StringUtils.isEmpty(item.getTaskKind())){
					continue;
				}
				
				Map<String, List<FdTimeoutConfig>> custConfig = timeoutConfigs.get(item.getTaskKind());
				if(custConfig == null){
					custConfig = new HashMap<String, List<FdTimeoutConfig>>();
					timeoutConfigs.put(item.getTaskKind(), custConfig);
				}
				List<FdTimeoutConfig> custConfigList = custConfig.get(item.getCustomer());
				if(custConfigList == null){
					custConfigList = new ArrayList<FdTimeoutConfig>();
					custConfig.put(item.getCustomer(), custConfigList);
				}
				custConfigList.add(item);
			}
		}
		
	}
	 
	
	/**
	 * 根据任务类型、客户获取超时控制信息
	 * @param taskKind
	 * @param customer
	 * @return
	 */
	public static List<FdTimeoutConfig> getTimeoutConfig(String taskKind, String customer){
		
		Map<String, List<FdTimeoutConfig>> timeoutConfig = timeoutConfigs.get(taskKind);
		if(timeoutConfig == null || timeoutConfig.isEmpty()){
			log.logError("no FdTimeoutConfig of taskKind:" + taskKind + ".");
			return null;
		}
		List<FdTimeoutConfig> customerConfigList = timeoutConfig.get(customer);
		if(customerConfigList == null){
			customerConfigList = timeoutConfig.get("default");			
		} 
		if(customerConfigList == null){
			log.logError("no FdTimeoutConfig of kind:" + taskKind + " for customer:" + customer + ".");
		}
		
		return customerConfigList;
	}
	
	/**
	 * 获取客户的缓存配置，如果不存在配置则返回null
	 * @param customer
	 * @return
	 */
	public static FdCustomerConfig getCustomerConfig(String customer){
		synchronized(customerConfig){			
			return customerConfig.get(customer);
		}
	
	}
	
	/**
	 * 根据错误码，获取缓存信息
	 * @param errorno
	 * @return
	 */
	public static String getErrorComment(String errorno){
		if(errorno == null || "".equals(errorno))
			return "";
		synchronized(errors){
			if(errors.containsKey(errorno)){
				return errors.get(errorno);
			}else{
				return "undefined error";
			}
		}
	}	
	 
	
	public static void main(String [] args){
	    WsfdCache.init();
	}
	
}
